package com.baijiayun.liveuibase.base;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import io.reactivex.disposables.CompositeDisposable;

/**
 * Created by pengwenjian on 2021/1/7
 * Describe:
 */
public class BaseLayer extends FrameLayout implements ILayer, LifecycleObserver {
    private Lifecycle lifecycle;
    protected CompositeDisposable compositeDisposable = new CompositeDisposable();

    public BaseLayer(@NonNull Context context) {
        super(context);
        init();
    }

    public BaseLayer(@NonNull Context context,@Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BaseLayer(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public BaseLayer(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    @CallSuper
    protected void init() {
        Context context = getContext();
        if (context instanceof RouterListener) {
            RouterListener routerListener = (RouterListener) context;
            lifecycle = routerListener.getLifecycle();
            lifecycle.addObserver(this);
        }
    }

    @Override
    public void addWindow(@Nullable IWindow window) {
        if (window != null) {
            addView(window.getView(), new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
    }

    @Override
    public void addWindow(@Nullable IWindow window, @NotNull FrameLayout.LayoutParams layoutParams) {
        if (window != null) {
            addView(window.getView(),layoutParams);
        }
    }

    @Override
    public void addWindow(@Nullable IWindow window, @NotNull ViewGroup.LayoutParams layoutParams) {
        if (window != null) {
            addView(window.getView(),layoutParams);
        }
    }

    @Override
    public void removeWindow(@Nullable IWindow window) {
        if (window != null) {
            removeView(window.getView());
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    @CallSuper
    protected void onDestroy() {
        compositeDisposable.clear();
        if (lifecycle != null) {
            lifecycle.removeObserver(this);
            lifecycle = null;
        }
    }

    public void removeAndDestroy(@Nullable BaseLayer layer) {
        if (layer != null) {
            removeView(layer);
            layer.onDestroy();
        }
    }

    @Nullable
    @Override
    public ViewGroup getViewGroup() {
        return this;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (lifecycle != null) {
            onDestroy();
        }
    }
}
